package com.snake.springboot.custom.service.impl.login;

import com.snake.springboot.custom.api.exception.LoginExpiresException;
import com.snake.springboot.custom.api.model.base.CustomAccountModel;
import com.snake.springboot.custom.api.model.login.LoginRedirectModel;
import com.snake.springboot.custom.api.service.login.CustomAccLoginService;
import com.snake.springboot.custom.dao.mybatis.mappers.base.CustMainAccountMapper;
import com.snake.springboot.custom.dao.mybatis.mappers.base.CustSubAccountMapper;
import org.snake.common.security.jwt.JwtHelper;
import org.snake.redis.commons.RedisPlusUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 类 {@code CustomAccLoginServiceImpl}  <br> .
 * <p>商户账号登录服务
 * <p>创建时间：2025/03/06
 *
 * @author Huang.yj
 */
@Service
public class CustomAccLoginServiceImpl implements CustomAccLoginService {
    private static String CustomLoginRedisKey = "Custom:LoginRedisKey:%s";
    private CustMainAccountMapper custMainAccountMapper;
    private CustSubAccountMapper custSubAccountMapper;
    private RedisPlusUtils redisPlusUtils;

    @Resource
    private JwtHelper custJwtHelper;

    @Autowired
    public CustomAccLoginServiceImpl(CustMainAccountMapper custMainAccountMapper,
                                     CustSubAccountMapper custSubAccountMapper,
                                     RedisPlusUtils redisPlusUtils){
        this.custMainAccountMapper = custMainAccountMapper;
        this.custSubAccountMapper = custSubAccountMapper;
        this.redisPlusUtils = redisPlusUtils;
    }

    public CustomAccountModel redirectLogin(LoginRedirectModel model){
        String token = model.getToken();
        CustomAccountModel customAccountModel = redisPlusUtils.getOpsVal(String.format(CustomLoginRedisKey,token)
                ,CustomAccountModel.class);
        if(customAccountModel!=null) return customAccountModel;
        else throw new LoginExpiresException("登录过期");
    }
}
